package com.cloudpivot.project.crmprojectnew.service.impl;

import com.cloudpivot.project.crmprojectnew.configuration.CrmConfig;
import com.cloudpivot.project.crmprojectnew.pojo.newproduct.NewProduct;
import com.cloudpivot.project.crmprojectnew.pojo.newproduct.NewProductList;
import com.cloudpivot.project.crmprojectnew.service.HttpAPIServiceForCrm;
import com.cloudpivot.project.crmprojectnew.service.NewProductService;
import com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户设备--设备名称(产品)
 */
@Service
@Slf4j
public class NewProductServiceImpl implements NewProductService {
    @Autowired
    private CrmConfig crmConfig;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    private static final String APISTR = "new_products";
    private static final String ENTITYSTR = "new_product";

    @Override
    public NewProduct getNewProduct(String guid) {
        String requestUrl = String.format("%s%s(%s)", this.crmConfig.getCrmurl(), APISTR, guid);
        try {
            String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl);
            NewProduct newProduct = objectMapper.readValue(responseStr, NewProduct.class);
            return newProduct;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 返回列表数据
     *
     * @param userid
     * @return
     */
    @Override
    public NewProductList getNewProducts(String userid, int pageIndex, int pageSize) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            NewProductList newProductList = new NewProductList();

            try {

                String requestUrl = CrmNormalUtil.urlEcode(String.format("%s%s?%s", crmConfig.getCrmurl(), APISTR, CrmNormalUtil.getFetchPage(pageIndex, pageSize, ENTITYSTR)));
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);

                newProductList = objectMapper.readValue(responseStr, NewProductList.class);

                return newProductList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取客户设备列表-名称,id搜索
     * 只可能返回一个结果
     *
     * @param userid
     * @param guid
     * @return
     */
    @Override
    public NewProductList getNewProducts(String userid, String guid) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            try {
                NewProductList newProductList = new NewProductList();
                List<NewProduct> tmpList = new ArrayList<>();
                String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                NewProduct doNewProduct = objectMapper.readValue(responseStr, NewProduct.class);
                tmpList.add(doNewProduct);

                newProductList.setNewProductList(tmpList);
                newProductList.setCount(tmpList.size());
                return newProductList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }
}
